package com.mapbased.wfw.components;

import java.io.*;
import java.util.*;

import org.dom4j.*;
import com.mapbased.wfw.*;
import javax.servlet.http.HttpSession;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2004</p>
 *
 * <p>Company: mapbased.com</p>
 *
 * @author chy_hs(Baily)
 * @version 1.0
 */
public class ShowInfo

{
    public ShowInfo()
    {
        super();
    }
    public static String stackTrace(RenderContext rc)
        throws java.io.IOException
    {
        com.mapbased.util.ThreadStack.print(rc);

        return "";
    }

    public static String excuteTime(RenderContext ctx)
    {
        Long l=(Long)ctx.getRequestMap().get(Constants.Page_Start_Time);
        if(l != null)
        {
            return(System.currentTimeMillis() - l.longValue()) + " ms";
        }
        return "Not fetch";

    }

    public static Map reqHeaderMap(RenderContext rc)
    {
        Map m=new java.util.HashMap();

        for(java.util.Enumeration e=rc.getRequest().getHeaderNames();
                                    e.hasMoreElements();)
        {
            Object o=e.nextElement();
            StringBuffer sb=new StringBuffer();
            for(java.util.Enumeration ee=rc.getRequest().getHeaders(o.toString());
                                         ee.hasMoreElements();)
            {
                sb.append("<p>").append(ee.nextElement()).append("</p>");
            }
            m.put(o,sb.toString());
        }
        return m;

    }

    public static Map sessionMap(RenderContext ctx)
    {
        HttpSession ss=ctx.getRequest().getSession(false);
        Map m=new java.util.HashMap();
        if(ss == null)
        {
            return m;
        }
        for(java.util.Enumeration e=ss.getAttributeNames();e.hasMoreElements();)
        {
            Object o=e.nextElement();
            m.put(o,ss.getAttribute(o.toString()));
        }
        m.put("getCreationTime",ss.getCreationTime() + "");

        m.put("getCreationTime",ss.getLastAccessedTime() + "");

        m.put("getId",ss.getId() + "");
        return m;

    }

    public static Map requestMap(RenderContext ctx)
    {
        Map m=new java.util.HashMap(ctx.getRequestMap());
        for(java.util.Iterator i=ctx.getRequestMap().entrySet().iterator();
                                 i.hasNext();)
        {
            Map.Entry e=(Map.Entry)i.next();
            Object o=e.getValue();
            if(o instanceof String[])
            {
                String[] ss=(String[])o;
                StringBuffer sb=new StringBuffer();
                for(int j=0;j < ss.length;j++)
                {
                    sb.append("<p>").append(ss[j]).append("</p>");
                }
                m.put(e.getKey(),sb.toString());
            }
        }
        return m;
    }
}
